﻿#include "Player.h"
#include "DataBaseThread.h"
#include "Config.h"
#include "CsvDataMgr.h"
#include "ClientHandler.h"
#include "TLog.h"
#include "StreamPacketBuilder.h"
#include "ActiveConnsList.h"
#include "GoodCardManager.h"

Player::Player(void)
{
	handler					= NULL ;
	//table_frame				= NULL ;
	m_table_id				= INVALID_TABLE ;
	m_chair_id				= INVALID_CHAIR ;
	//m_packet_count			= 0 ;
	m_status				= PLAYER_ONLINE ;
	m_timer_id				= 0 ;
	m_user_info.user_status = STATUS_IDLE ;
	//m_user_type				= PLAYER_NORMAL ;
	m_play_times			= 0 ;
	m_props_count			= 0 ;
	m_play_free_time		= 0 ;
	m_slot_play_info.reset();
	m_roulette_play_info.reset();
	m_game_status_time		= (int)time(NULL);
	reflash_active_time();
	m_stay_table_time		= 0;
	m_allin_coin			= 0 ;
	m_table					= NULL ;
	m_table_coin			= 0 ;
	m_last_roomid			= 0;
	m_is_load_tasks			= false;
	m_is_load_hongbao_tasks			= false;	
	m_is_need_clear_task	= false;
	m_is_load_friends		= false;
	m_isadmin = false;
	InitPlayerTasks();
	InitPlayerHongbaoTasks();

	m_enemy_list.clear();
	
	for (int i = 0; i < MAX_ENEMY_COUNT; ++i) {
		m_enemy_list.push_back(&m_enemy_info[i]);
	}
	m_is_look_card = false;
	m_friend_list.clear();
	m_last_tableid = INVALID_TABLE;

	m_nn_qiang_rate = 0xFF;		
	m_nn_jiazhu_rate = 0xFF;		
	m_nn_calcu_rate = 0xFF;	
	m_nn_times = 0xFF;
	
}
DWORD Player::get_prop_count( int prop_id ){
	if ( m_props == NULL ) return 0 ;

	int prop_count = Config::instance()->m_prop_count ;
	for ( int i = 0 ; i < prop_count ; i++)
	{
		if ( m_props [ i ].prop_id == prop_id && m_props [ i ].prop_remain_day > 0 ){
			return m_props [ i ].prop_count ;
		}
	}
	return 0 ;
}

void Player::set_prop_count( int prop_id , DWORD prop_count ){
	int prop_cn = Config::instance()->m_prop_count ;
	if ( m_props == NULL ) {
		set_props( 1 ) ;
		m_props [ 0 ].prop_id = prop_id ;
		m_props [ 0 ].prop_count = prop_count ;
		return ;
	}
	for ( int i = 0 ; i < prop_cn ; i++)
	{
		if ( m_props [ i ].prop_id == prop_id  ){
			m_props [ i ].prop_count = prop_count ;
			return ;
		}
	}
}
Player::~Player(void)
{
	MAP_TASK::iterator it = m_tasks.begin();
	m_tasks.clear();
	m_hongbao_tasks.clear();
	m_friend_list.clear();
	m_enemy_list.clear();
}

void Player::startGame()
{
	 m_user_info.user_status = PLAYER_STATUS_PLAY ;
	 m_table_coin = 0 ;
	 m_allin_coin = 0 ;
	 m_game_status_time = (int)time(NULL);

	 m_nn_qiang_rate = 0xFF;		
	m_nn_jiazhu_rate = 0xFF;		
	m_nn_calcu_rate = 0xFF;	
	m_nn_times = 0xFF;
}

void Player::OnEnterGame(bool is_enter_game)
{
}


void Player::InitEveryDayVar( DB_CLEAR_LASTDAY_VAR *msg )
{
	//printf( "zq InitEveryDayVar() ======================  msg->logintime = %d\r\n", msg->logintime );
	m_user_info.last_login_time = msg->logintime;
	m_user_info.today_get_help_count = msg->helpcount;
	m_user_info.tody_get_login_box_count = msg->onlineboxcount;
	m_user_info.today_game_counts = msg->playcount;
	m_user_info.today_win_counts = msg->wincount;
	m_user_info.lotterydrownum = msg->drawcount;
	ClearDayTask();
}

TaskInfo * Player::GetHongbaoTask(int taskid)
{
	MAP_TASK::iterator it = m_hongbao_tasks.find(taskid);
	if (it == m_hongbao_tasks.end())
	{
		return NULL;
	}
	return &(it->second);
}

TaskInfo * Player::GetTask(int taskid)
{
	MAP_TASK::iterator it = m_tasks.find(taskid);
	if (it == m_tasks.end())
	{
		return NULL;
	}
	return &(it->second);
}

TaskInfo * Player::GetFinishTaskByAttr( int taskattr, int value )
{
	MAP_TASK::iterator it;
	for ( it = m_tasks.begin(); it != m_tasks.end(); it++ )
	{
		if ( (it->second.taskattr == taskattr) && (it->second.taskvalue == value) )
		{
			return &(it->second);
		}
	}
	return NULL;
}

bool Player::IsCanFinish(TaskInfo * taskinfo, PLAYER_TASK_STRUCT * csv_task)
{
	if (taskinfo == NULL || csv_task == NULL)
		return false;

	if (taskinfo->taskstatus == TASK_STATUS_CANFINISH)
		return true;

	switch (csv_task->task_attribute)
	{
	case 1:
		break;
	case 2:
		break;
	case 3:
		return m_user_info.total_pay_num >= csv_task->task_value;
	case 4:
		break;
	case 5:
		return m_user_info.total_use_laba_counts >= csv_task->task_value;
	case 6:
		return m_user_info.today_win_counts >= csv_task->task_value;
	case 7:
		return m_user_info.today_game_counts >= csv_task->task_value;
	case 8:
		return m_user_info.total_win_counts >= csv_task->task_value;
	case 9:
		return m_user_info.total_primary_win_counts >= csv_task->task_value;
	case 10:
		return m_user_info.total_middle_win_counts >= csv_task->task_value;
	case 11:
		return m_user_info.total_high_win_counts >= csv_task->task_value;
	default:
		break;
	}

	return false;
}

void Player::InitHongbaoTasks(char * pData, int data_len)
{
	m_is_load_hongbao_tasks = true;

	if (pData == NULL || data_len < 2) {
		return;
	}

	StreamReader reader( pData );
	WORD	tasknum	= reader.get_short();
	//printf( "zq tasknums=%d  m_tasks.size() = %d\r\n", tasknum, m_tasks.size() );
	if (tasknum == 0)
		return;

	TaskInfo * pTaskinfo = NULL;
	for (WORD i = 0; i < tasknum; ++i) {
		int id = reader.get_int();
		BYTE status = reader.get_byte();
		int finishtime = reader.get_int();
		int value = reader.get_int();
		pTaskinfo = GetHongbaoTask(id);
		if (pTaskinfo == NULL)
			continue;
		pTaskinfo->taskstatus = status;
		pTaskinfo->finishtime = finishtime;
		pTaskinfo->taskvalue = value;
	}
}

void Player::UpdateHongbaoTask(HONGBAO_TASK_TYPE type, int sub_type, int value)
{
	bool dirty = false;

	PLAYER_HONGBAO_TASK_MAP* conf_map = Config::instance()->GetHongbaoTaskMap();
	for (PLAYER_HONGBAO_TASK_MAP::iterator it = conf_map->begin();
		 it != conf_map->end(); ++it)
	{
		PLAYER_HONGBAO_TASK_STRUCT* taskconf = (it)->second;
		if (!taskconf || taskconf->task_typeid != type || taskconf->task_sub_typeid != sub_type)
			continue;
		int taskid = taskconf->taskid;
		TaskInfo * pTaskinfo = NULL;
		pTaskinfo = GetHongbaoTask(taskid);
		if (pTaskinfo == NULL)
			continue;
		if (pTaskinfo->taskstatus != TASK_STATUS_ACCEPTED)
			continue;
		dirty = true;
		pTaskinfo->taskvalue += value;
		if (pTaskinfo->taskvalue >= taskconf->task_value)
			pTaskinfo->taskstatus = TASK_STATUS_CANFINISH;

		SQLReq * req = SQLMemoryPool::instance()->mallocReq();
		req->id = get_user_id();
		snprintf( req->sql , sizeof(req->sql) ,"call p_save_hongbao_task(%u, %d, %d, %d)", get_user_id(), taskid, pTaskinfo->taskstatus, pTaskinfo->taskvalue );
		WL(LL_DEV , "handleUpdateTask req->sql = %s\r\n", req->sql );
		req->client		= NULL ;
		req->req_type	= REQ_TYPE_EXECUTE_COMMOND;
		DataBaseReadThreadPool::instance()->pushQueue( req );
	}

	if (dirty)
		SendHongbaoTasksToClient();	
}

void Player::InitTasks(char * pData, int data_len)
{
	m_is_load_tasks = true;

	if (pData == NULL || data_len < 2) {
		return;
	}

	StreamReader reader( pData );
	WORD	tasknum	= reader.get_short();
	//printf( "zq tasknums=%d  m_tasks.size() = %d\r\n", tasknum, m_tasks.size() );
	if (tasknum == 0)
		return;

	TaskInfo * pTaskinfo = NULL;
	for (WORD i = 0; i < tasknum; ++i) {
		int id = reader.get_int();
		BYTE status = reader.get_byte();
		int finishtime = reader.get_int();
		pTaskinfo = GetTask(id);
		if (pTaskinfo == NULL)
			continue;
		pTaskinfo->taskstatus = status;
		pTaskinfo->finishtime = finishtime;
	}
	//判断每日任务是否完成
	ClearDayTask();
}

void Player::InitPlayerHongbaoTasks()
{
	m_hongbao_tasks.clear();
	PLAYER_HONGBAO_TASK_MAP * pTaskMap = Config::instance()->GetHongbaoTaskMap();
		//printf( "zq pTaskMap size() = %d\r\n", pTaskMap->size() );
	PLAYER_HONGBAO_TASK_MAP::iterator it = pTaskMap->begin();
	for (; it != pTaskMap->end(); ++it) {
		if (it->second == NULL)
			continue;
		TaskInfo tmpobj;
		tmpobj.taskid = it->second->taskid;
		tmpobj.taskstatus = TASK_STATUS_ACCEPTED;
		tmpobj.taskattr = it->second->task_attribute;
		tmpobj.taskvalue = 0;//it->second->task_value;
		tmpobj.finishtime = 0;
		m_hongbao_tasks.insert(MAP_TASK::value_type(tmpobj.taskid, tmpobj));
	}
}

//玩家进入游戏初始化任务
void Player::InitPlayerTasks()
{
	m_tasks.clear();
	PLAYER_TASK_MAP * pTaskMap = Config::instance()->GetTaskMap();
	//printf( "zq pTaskMap size() = %d\r\n", pTaskMap->size() );
	PLAYER_TASK_MAP::iterator it = pTaskMap->begin();
	for (; it != pTaskMap->end(); ++it) {
		if (it->second == NULL)
			continue;
		TaskInfo tmpobj;
		tmpobj.taskid = it->second->taskid;
		tmpobj.taskstatus = TASK_STATUS_ACCEPTED;
		tmpobj.taskattr = it->second->task_attribute;
		tmpobj.taskvalue = it->second->task_value;
		tmpobj.finishtime = 0;
		m_tasks.insert(MAP_TASK::value_type(tmpobj.taskid, tmpobj));
	}

	//WL(LL_INFO , "InitTasksByCsv  tasknum = %d, %d \n", m_tasks.size(), pTaskMap->size()  );
}

void Player::SendHongbaoTasksToClient()
{
	u8_t  taskdata [ 8192 ]	;
	StreamWriter stream_out(taskdata, sizeof(taskdata));

	WORD tasknum = (WORD)m_hongbao_tasks.size();
	stream_out.add_word( tasknum );
	
	TaskInfo * pTaskinfo = NULL;
	MAP_TASK::iterator it = m_hongbao_tasks.begin();
	//printf( "m_tasks:%d\r\n", m_tasks.size() );
	for ( ; it != m_hongbao_tasks.end(); ++it ) {
		pTaskinfo = &(it->second);
		if (pTaskinfo == NULL)
		{
			WL(LL_ERROR , "SendTasksToClient playerid =%u failed, can not find task %d \n", get_user_id(), it->first);
			continue;
		}
		stream_out.add_int( pTaskinfo->taskid ) ;
		stream_out.add_byte( pTaskinfo->taskstatus ) ;
		stream_out.add_int( pTaskinfo->taskvalue ) ;
//		WL(LL_DEV , "SendTasksToClient playerid =%u; taskid = %d status=%d value = %d\n", get_user_id(), pTaskinfo->taskid, pTaskinfo->taskstatus, pTaskinfo->taskvalue);
//		printf( "%s: player[%u] taskid = %d, status = %d, value = %d\r\n", __FUNCTION__, get_user_id(), pTaskinfo->taskid, pTaskinfo->taskstatus, pTaskinfo->taskva	\
lue );
	}

	//WL(LL_INFO , "SendTasksToClient playerid =%u; tasknum = %d,  len=%d\n", get_user_id(),  tasknum , stream_out.get_buffer_len()  );
	FrontClientHandler * pHandle = get_handler();
	if (pHandle)
		pHandle->sendPacket( MSG_RESP_HONGBAO_TASK_LIST, taskdata, stream_out.get_buffer_len() );
}

void Player::SendTasksToClient()
{
	u8_t  taskdata [ 8192 ]	;
	StreamWriter stream_out(taskdata, sizeof(taskdata));

	WORD tasknum = (WORD)m_tasks.size();
	stream_out.add_word( tasknum );
	
	TaskInfo * pTaskinfo = NULL;
	MAP_TASK::iterator it = m_tasks.begin();
	//printf( "m_tasks:%d\r\n", m_tasks.size() );
	for ( ; it != m_tasks.end(); ++it ) {
		pTaskinfo = &(it->second);
		if (pTaskinfo == NULL)
			continue;
		stream_out.add_int( pTaskinfo->taskid ) ;
		stream_out.add_byte( pTaskinfo->taskstatus ) ;
		//printf( "taskid = %d status = %d\r\n", pTaskinfo->taskid, pTaskinfo->taskstatus );
	}

	//WL(LL_INFO , "SendTasksToClient playerid =%u; tasknum = %d,  len=%d\n", get_user_id(),  tasknum , stream_out.get_buffer_len()  );
	FrontClientHandler * pHandle = get_handler();
	if (pHandle)
		pHandle->sendPacket( MSG_RESP_TASK_LIST, taskdata, stream_out.get_buffer_len() );
}

void Player::ClearDayTask()
{
	MAP_TASK_ITOR iter;
	DWORD daystarttime = time(NULL) - (time(NULL) + 3600*8)%(3600*24);	//0点时间
	//printf( "ClearDayTask daystarttime = %d\r\n", daystarttime );
	for ( iter = m_tasks.begin(); iter != m_tasks.end(); iter++ )
	{
		if ( (iter->second.taskattr == 6) || (iter->second.taskattr == 7) )
		{
			if ( iter->second.finishtime < daystarttime )
			{
				//printf( "ClearDayTask taskid = %d status = %d finishtime = %d\r\n", iter->second->taskid,  iter->second->taskstatus, iter->second->finishtime  );
				iter->second.taskstatus = TASK_STATUS_ACCEPTED;
			}
		}
	}
}

void Player::check_robot_gold()
{
	if (!isNormalPlayer())
	{
		// if (get_user_gold() < 1000 && m_table)
		// {
		// 	m_table->leaveTable(this);
		// }
		INT64 gold = get_user_gold();
		if (gold < 2000)
		{
			add_user_gold(40000);

			SQLReq * req = SQLMemoryPool::instance()->mallocReq( REQ_TYPE_DDL,this );
			snprintf( req->sql , sizeof(req->sql),"call p_rechange_gold(%u,%lld,%d)",get_user_id(),get_user_score()->gold , 40000);
			add_user_gold( 40000 );
			DataBaseCommitThread::instance()->push( req ) ;
			return;
		}

		if (!m_table)
			return;
		RoomConfigItem * roomconf = m_table->getRoomConf();
		if (!roomconf)
			return;
		
		switch (roomconf->room_id)
		{
			case ROOM_CONFI_ID_1:
			{
				if (gold > 1000000)
				{
//					5000 - 500000
					int rand_val = rand() % (50000 - 5000) + 5000;
					INT64 add_gold = rand_val - gold;
					add_user_gold(add_gold);
					SQLReq * req = SQLMemoryPool::instance()->mallocReq( REQ_TYPE_DDL,this );
					snprintf( req->sql , sizeof(req->sql),"call p_rechange_gold(%u,%lld,%d)",get_user_id(), gold , add_gold);
					DataBaseCommitThread::instance()->push( req ) ;
				}
				break;
			}
			case ROOM_CONFI_ID_2:
			{
				if (get_user_gold() > 5000000)
				{
//					50000 - 1000000
					int rand_val = rand() % (1000000 - 50000) + 50000;
					INT64 add_gold = rand_val - gold;
					add_user_gold(add_gold);
					SQLReq * req = SQLMemoryPool::instance()->mallocReq( REQ_TYPE_DDL,this );
					snprintf( req->sql , sizeof(req->sql),"call p_rechange_gold(%u,%lld,%d)",get_user_id(), gold , add_gold);
					DataBaseCommitThread::instance()->push( req ) ;
				}
				break;
			}
			case ROOM_CONFI_ID_3:
			case ROOM_CONFI_ID_5:
			{
				if (gold < 500000 || gold > 10000000)
				{
//					1000000 - 10000000
					int rand_val = rand() % (10000000 - 1000000) + 1000000;
					INT64 add_gold = rand_val - gold;
					add_user_gold(add_gold);
					SQLReq * req = SQLMemoryPool::instance()->mallocReq( REQ_TYPE_DDL,this );
					snprintf( req->sql , sizeof(req->sql),"call p_rechange_gold(%u,%lld,%d)",get_user_id(), gold , add_gold);
					DataBaseCommitThread::instance()->push( req ) ;
				}
				break;
			}
		}		
	}
}

void Player::OnPlayerLost()
{
	m_user_info.today_game_counts++;
	set_user_lost_count( get_user_lost_count() + 1 );
	OnJuShuTask( 7, m_user_info.today_game_counts );//每日任务对战局
	check_robot_gold();
}

void Player::OnPlayerWin()
{
	m_user_info.total_win_counts++;
	m_user_info.today_win_counts++;
	m_user_info.today_game_counts++;
	set_user_win_count( get_user_win_count() + 1 );
	if (m_table) {
		RoomConfigItem * roomconf = m_table->getRoomConf();
		if (roomconf) {
			switch (roomconf->room_id)
			{
			case ROOM_CONFI_ID_1:
			case ROOM_CONFI_ID_20:
				m_user_info.total_primary_win_counts++;
				OnJuShuTask( 9, m_user_info.total_primary_win_counts );
				break;
			case ROOM_CONFI_ID_2:
			case ROOM_CONFI_ID_21:
				m_user_info.total_middle_win_counts++;
				OnJuShuTask( 10, m_user_info.total_middle_win_counts );
				break;
			case ROOM_CONFI_ID_3:
			case ROOM_CONFI_ID_22:
				m_user_info.total_high_win_counts++;
				OnJuShuTask( 11, m_user_info.total_high_win_counts );
				break;
			default:
				break;
			}
		}
	}
	//任意场赢多少场
	OnJuShuTask( 8, get_user_win_count() );
	OnJuShuTask( 6, m_user_info.today_win_counts );//每日任务赢局
	OnJuShuTask( 7, m_user_info.today_game_counts );//每日任务对战局
}

void Player::OnGetShunjin()
{
	int taskid = 1201;
	TaskInfo * pTask = GetTask(taskid);
	if (pTask == NULL)
		return;
	//printf( "==========OnGetShunjin taskattr = %d  jushu = %d pTask = %x\r\n", 0, 0, pTask );
	if (pTask->taskstatus == TASK_STATUS_ACCEPTED) {
		pTask->taskstatus  = TASK_STATUS_CANFINISH;

		if (get_handler())
			get_handler()->handleUpdateTask(pTask, 0);
	}
}

void Player::OnGetBaozi()
{
	int taskid = 1200;
	TaskInfo * pTask = GetTask(taskid);
	//printf( "==========OnGetBaozi taskattr = %d  jushu = %d pTask = %x\r\n", 0, 0, pTask );
	if (pTask == NULL)
		return;

	if (pTask->taskstatus == TASK_STATUS_ACCEPTED) {
		pTask->taskstatus  = TASK_STATUS_CANFINISH;

		if (get_handler())
			get_handler()->handleUpdateTask(pTask, 0);
	}
}

void Player::OnJuShuTask( int taskattr, int jushu )
{
	TaskInfo * pTask = GetFinishTaskByAttr( taskattr, jushu );
	//printf( "==========OnJuShuTask taskattr = %d  jushu = %d pTask = %x\r\n",taskattr, jushu, pTask );
	if (pTask == NULL)
		return;

	if (pTask->taskstatus == TASK_STATUS_ACCEPTED)
	{
		pTask->taskstatus  = TASK_STATUS_CANFINISH;
		pTask->finishtime = time( NULL );
		if (get_handler())
			get_handler()->handleUpdateTask(pTask, 0);
	}
}

void Player::OnChangeHeadPicture()
{
	int taskid = 1000;
	TaskInfo * pTask = GetTask(taskid);
	if (pTask == NULL)
		return;

	if (pTask->taskstatus == TASK_STATUS_ACCEPTED) {
		pTask->taskstatus  = TASK_STATUS_CANFINISH;

		if (get_handler())
			get_handler()->handleUpdateTask(pTask, 0);
	}
}

void Player::OnUseLaba()
{
	m_user_info.total_use_laba_counts++;
	if ( m_user_info.total_use_laba_counts== 1 )
	{
		int taskid = 1001;
		TaskInfo * pTask = GetTask(taskid);
		if (pTask == NULL)
			return;
		if (pTask->taskstatus == TASK_STATUS_ACCEPTED) {
			pTask->taskstatus  = TASK_STATUS_CANFINISH;

			if (get_handler())
				get_handler()->handleUpdateTask(pTask, 0);
		}
	}
	
}

void Player::OnWinMoney(DWORD win_golds, int card_type)
{
	int need_gold = 10000;
	int roomid = GetCurRoomID();
	std::string roomstr;
	switch (roomid)
	{
	case ROOM_CONFI_ID_1:
		roomstr = "初级场";
		break;
	case ROOM_CONFI_ID_2:
		roomstr = "中级场";
		break;
	case ROOM_CONFI_ID_3:
		roomstr = "高级场";
		break;
	case ROOM_CONFI_ID_4:
		roomstr = "私人房";
		break;
	case ROOM_CONFI_ID_5:
		roomstr = "土豪场";
		roomid = 9;	//9表示土豪场
		break;
	case ROOM_CONFI_ID_8:
		roomstr = "闷牌场";
		roomid = 11;
		break;
	default:
		return;
	}
	
	need_gold = Config::instance()->get_send_horn_min_gold( roomid );

	if (win_golds < need_gold) // TODO  配置表
		return;

	std::string cardstr;
	switch (card_type)
	{
	case CT_SINGLE:
		cardstr = "单牌";
		break;
	case CT_DOUBLE:
		cardstr = "对子";
		break;
	case CT_SHUN_ZI:
		cardstr = "顺子";
		break;
	case CT_JIN_HUA:
		cardstr = "金花";
		break;
	case CT_SHUN_JIN:
		cardstr = "顺金";
		break;
	case CT_BAO_ZI:
		cardstr = "豹子";
		break;
	case CT_SPECIAL:
		cardstr = "特殊牌型";
		break;
	default:
		return;
	}

	char szbuff[512] = {0};
	const char *contentformat = Config::instance()->get_horn_format( HORN_FORMAT_WIN );
	if ( contentformat )
	{
		snprintf(szbuff, sizeof(szbuff), contentformat, get_nick_name(), roomstr.c_str(), cardstr.c_str(), win_golds);
		StreamPacketBuilder::instance()->notifySystemBroadcast((u8_t *)szbuff, strlen(szbuff)+1, get_user_id() );
	}
}


int	Player::GetCurRoomID()
{
	if (m_table == NULL)
		return 0;

	RoomConfigItem * pConfig = m_table->getRoomConf();
	if (pConfig == NULL)
		return 0;

	return pConfig->room_id;
}

int	Player::GetFriendCount()
{
	return m_friend_list.size();
}

bool Player::IsCanAddFirend()
{
	//根据vip级别来添加好友
	int viplevel = m_user_info.viplevel;
	int maxfriendnum = Config::instance()->m_vipconfigmap[viplevel].maxfriend;
	
	if ( GetFriendCount() >= maxfriendnum  )
		return false;
	return true;
}

FriendInfo* Player::AddFriend(DWORD friendid, long long add_time, int sex, INT64 gold, char* nickname, int nicknamelen, char* headpic, int headpiclen, char *sign, int signlen )
{
	//printf( "friendid = %d, nicknamelen = %d headpiclen = %d signlen = %d\r\n", friendid, nicknamelen, headpiclen, signlen );
	if (friendid <= 0)
		return NULL;
	
	for (int i = 0; i < MAX_FRIEND_COUNT; ++i)
	{
		if ( m_friends_info[i].userid == friendid )
		{
			return &m_friends_info[i];
		}
		if ( m_friends_info[i].userid > 0 )
		{
			continue;
		}
		m_friends_info[i].userid = friendid;
		m_friends_info[i].add_time = add_time;
		m_friends_info[i].sex = sex;
		m_friends_info[i].golds = gold;
		memcpy(m_friends_info[i].nick_name, nickname, nicknamelen);
		memcpy(m_friends_info[i].head_picture, headpic, headpiclen);
		memcpy(m_friends_info[i].char_sign, sign, signlen);
		return &m_friends_info[i];
	}
	return NULL;
}

FriendInfoEx * Player::AddFriendEx(DWORD friendid, long long add_time, UserInfo & userinfo)
{
	printf( "friendid = %d \r\n", friendid );
	if (friendid <= 0)
		return NULL;

	FriendInfoEx friendmsg = {0};
	memcpy( friendmsg.nick_name, userinfo.nick_name, sizeof(friendmsg.nick_name) - 1 );
	friendmsg.userid = friendid;
	m_friend_list.push_back( friendmsg );
	
	return &m_friend_list[m_friend_list.size()-1];
}

bool Player::DeleteFriend(DWORD friendid)
{
	vector<FriendInfoEx>::iterator iter;
	for ( iter = m_friend_list.begin(); iter != m_friend_list.end(); iter++ )
	{
		if ( iter->userid == friendid )
		{
			m_friend_list.erase( iter );
			return true;
		}
	}
	return false;
}

FriendInfoEx * Player::GetFriendInfo(DWORD friendid)
{
	vector<FriendInfoEx>::iterator iter;
	for ( iter = m_friend_list.begin(); iter != m_friend_list.end(); iter++ )
	{
		if ( iter->userid == friendid )
		{
			return (FriendInfoEx*)&(*iter);
		}
	}
	return NULL;
}

FriendInfo * Player::GetEmptyFriendInfo()
{
	for (int i = 0; i < MAX_FRIEND_COUNT; ++i) {
		if (m_friends_info[i].userid > 0)
			continue;	

		return &m_friends_info[i];
	}

	return NULL;
}

void Player::InitFriendFromDB(char * pData, int data_len)
{
	m_is_load_friends = true;
	StreamReader reader( pData );
	WORD friend_count = reader.get_short();
	if (friend_count == 0)
	{
		return;
	}
	for ( int i = 0; i < friend_count; i++ )
	{
		int userid = reader.get_int();
		INT64 addtime = reader.get_int64();
		int nicknema_len = reader.get_short();
		char* nickname = reader.get_current_ptr();
		reader.skip(nicknema_len);
		int headpiclen = reader.get_short();
		char* headpicptr = reader.get_current_ptr();
		reader.skip(headpiclen);
		INT64 friendgold = reader.get_int64();
		BYTE sex = reader.get_byte();
		int signlen = reader.get_short();
		char* signptr = reader.get_current_ptr();
		reader.skip(signlen);
		int msgcount = reader.get_int();

		//旧版本保存20条数据
		AddFriend( userid, addtime, sex, friendgold, nickname, nicknema_len, headpicptr, headpiclen,  signptr, signlen );

		vector<FriendInfoEx>::iterator iter;
		bool isfind = false;
		for ( iter = m_friend_list.begin(); iter != m_friend_list.end(); iter++ )
		{
			if ( iter->userid == userid )
			{
				nicknema_len = (nicknema_len >= NAME_LENGTH) ? (NAME_LENGTH - 1) : nicknema_len;
				memcpy( iter->nick_name, nickname, nicknema_len );
				iter->msgcount = msgcount;
				isfind = true;
				break;
			}
		}
		//如果没找到
		if ( isfind == false)
		{
			int tableid = INVALID_TABLE;
			FriendInfoEx friendmsg = {0};
			friendmsg.msgcount = msgcount;
			friendmsg.userid = userid;
			nicknema_len = (nicknema_len >= NAME_LENGTH) ? (NAME_LENGTH - 1) : nicknema_len;
			memcpy( friendmsg.nick_name, nickname, nicknema_len );
			m_friend_list.push_back( friendmsg );
		}
		FrontClientHandler * pFriendHandle = ActiveConnsList::instance()->find( userid );
		if ( pFriendHandle )
		{
			FriendStatus playerstatus = {0};
			playerstatus.tableid = 0;
			playerstatus.status = 1;
			playerstatus.userid = m_user_info.user_id;
			//printf( " LOAD  status = %d, frienduserid = %d  userid = %d tableid = %d\r\n", playerstatus.status, iter->userid, get_user_id(), playerstatus.tableid );
			pFriendHandle->sendPacket( MSG_NOTIFY_FRIEND_STATUS, (u8_t*)&playerstatus, sizeof(playerstatus) );
		}
	}
	//printf( "InitFriendFromDB m_friend_list sizeof() = %d\r\n", m_friend_list.size() );
}

//用户主动请求
void Player::SendFriendToClient()
{
	u8_t  tmpdata [ 8192 ]	;
	StreamWriter stream_out(tmpdata, sizeof(tmpdata));

	WORD add_num = 0;
	for (int i = 0; i < MAX_FRIEND_COUNT; ++i)
	{
		if ( m_friends_info[i].userid > 0 )
		{
			add_num++;
			continue;
		}
	}
	stream_out.add_word(add_num);

	FrontClientHandler * pFriendHandle = NULL;
	Player * pFriendPlayer = NULL;
	WORD tmplen = 0;
	int onefrienddatalen = 2;
	//printf( "=======================================\r\n" );
	for (int i = 0; i < MAX_FRIEND_COUNT; ++i)
	{
		if (m_friends_info[i].userid <= 0)
			continue;
		//printf( "zq m_friends_info[%d].userid = %u\r\n", i, m_friends_info[i].userid );
		pFriendHandle = ActiveConnsList::instance()->find(m_friends_info[i].userid);
		//printf( "id : %d  status : %s\r\n", m_friends_info[i].userid, pFriendHandle == NULL ? "offline" : "online" );
		stream_out.add_uint( 0 ) ;
		stream_out.add_uint( m_friends_info[i].userid ) ;
		stream_out.add_int64( m_friends_info[i].add_time ) ;
		//printf( "1friend nick %s pFriendHandle = %x \r\n", m_friends_info[i].nick_name, pFriendHandle );
		if ( (pFriendHandle) && ( pFriendHandle->getPlayer()) )
		{
			memcpy( m_friends_info[i].nick_name, pFriendHandle->getPlayer()->get_nick_name(), sizeof(m_friends_info[i].nick_name) );
			memcpy( m_friends_info[i].char_sign, pFriendHandle->getPlayer()->get_char_sign(), sizeof(m_friends_info[i].char_sign) );
			m_friends_info[i].sex = pFriendHandle->getPlayer()->get_sex();
		}
		tmplen = strlen(m_friends_info[i].nick_name);
		stream_out.add_word(tmplen);
		stream_out.appand(m_friends_info[i].nick_name, tmplen);
		//printf( "2friend nick %s\r\n", m_friends_info[i].nick_name );
		tmplen = strlen(m_friends_info[i].head_picture);
		stream_out.add_word(tmplen);
		stream_out.appand(m_friends_info[i].head_picture, tmplen);
		stream_out.add_byte(m_friends_info[i].sex);

		if (pFriendHandle != NULL) 
		{
			pFriendPlayer = pFriendHandle->getPlayer();
			if (pFriendPlayer) {
				stream_out.add_int64(pFriendPlayer->get_user_gold());
				stream_out.add_byte(1);
				stream_out.add_int(pFriendPlayer->get_table_id());
				stream_out.add_int(pFriendPlayer->GetCurRoomID());
			}
			else {
				stream_out.add_int64(m_friends_info[i].golds);
				stream_out.add_byte(0);
				stream_out.add_int(0);
				stream_out.add_int(0);
			}
		}
		else {
			stream_out.add_int64(m_friends_info[i].golds);
			stream_out.add_byte(0);
			stream_out.add_int(0);
			stream_out.add_int(0);
		}

		tmplen = strlen(m_friends_info[i].char_sign);
		stream_out.add_word(tmplen);
		stream_out.appand(m_friends_info[i].char_sign, tmplen);
		stream_out.add_int( m_friends_info[i].msgcount );
		int datalen =  stream_out.get_buffer_len() - onefrienddatalen;
		int* lenptr = (int*)(stream_out.get_buffer() + onefrienddatalen);
		*lenptr = datalen;
		//printf( "i=%d *lenptr = %d userid = %d msgcount = %d nick_name = %s  len = %d  onelen = %d\r\n",
		//	i, *lenptr, m_friends_info[i].userid, m_friends_info[i].msgcount,m_friends_info[i].nick_name, stream_out.get_buffer_len(), onefrienddatalen );
		onefrienddatalen += datalen;
	}
	//printf( "*****************************************************\r\n" );
	FrontClientHandler * pHandle = get_handler();
	if (pHandle)
	{
		pHandle->sendPacket( MSG_RESP_GET_FRIEND_LIST, tmpdata, stream_out.get_buffer_len() );
		//printf( "zq send friend data to client!! \r\n" );
	}
}

//用户主动请求
void Player::SendFriendToClientEx()
{
	FrontClientHandler * pHandle = get_handler();
	if ( !pHandle )
	{
		return;
	}
	vector<FriendInfoEx>::iterator iter;
	int friendcount = m_friend_list.size();
	//printf( "zq  userid  = %d friendcount = %d\r\n", get_user_id(), friendcount );
	for ( int i = 0; i <= friendcount/50 ; i++ )
	{
		int startpos = i*50;
		int endpos = (i+1)*50 > friendcount ? friendcount : (i+1)*50;//计算数量
		int sendfriendcount = endpos - startpos;

		//printf( "startpos = %d endpos = %d friendcount = %d\r\n", startpos, endpos, friendcount );

		u8_t  tmpdata [ 8192 ] = {0};
		StreamWriter stream_out(tmpdata, sizeof(tmpdata));
		stream_out.add_word( sendfriendcount );
		for ( int j = startpos; j < endpos; j++ )
		{
			FrontClientHandler * pFriendHandle = ActiveConnsList::instance()->find(m_friend_list[j].userid);
			int playerstatus = 0;
			int tableid = INVALID_TABLE;
			if ( pFriendHandle && pFriendHandle->getPlayer() )
			{	
				//在线
				playerstatus = 1;
				tableid = pFriendHandle->getPlayer()->get_table_id();
				if ( tableid > 0 )
				{
					playerstatus = 2;//在游戏中
				}
			}
			stream_out.add_uint( m_friend_list[j].userid );
			stream_out.add_byte( strlen(m_friend_list[j].nick_name) );
			stream_out.appand( m_friend_list[j].nick_name, strlen(m_friend_list[j].nick_name) );
			stream_out.add_int( tableid );
			stream_out.add_byte( playerstatus );
			int msgcount = m_friend_list[j].msgcount;
			msgcount = msgcount >= 255 ? 255 : msgcount;
			stream_out.add_byte( msgcount );
			m_friend_list[j].msgcount = 0;
		}
		//printf( "send friend data to client\r\n" );
		pHandle->sendPacket( MSG_RESP_FRIEND_LIST, stream_out.get_buffer(), stream_out.get_buffer_len() );
	}

}

void Player::SendOneFriendToClient(UserInfo * pUserInfo)
{
	if (pUserInfo == NULL)
		return;

	u8_t  tmpdata [ 1024 ] = {0};
	StreamWriter stream_out(tmpdata, sizeof(tmpdata));
	
	WORD tmplen = 0;
			
	stream_out.add_uint( pUserInfo->user_id ) ;
	tmplen = strlen(pUserInfo->nick_name);
	stream_out.add_word(tmplen);
	stream_out.appand(pUserInfo->nick_name, tmplen);
	tmplen = strlen(pUserInfo->head_picture);
	stream_out.add_word(tmplen);
	stream_out.appand(pUserInfo->head_picture, tmplen);
	stream_out.add_byte(pUserInfo->user_sex);
	
	FrontClientHandler * pFriendHandle = ActiveConnsList::instance()->find(pUserInfo->user_id);
	if (pFriendHandle != NULL) {
		Player * pFriendPlayer = pFriendHandle->getPlayer();
		if (pFriendPlayer) {
			stream_out.add_int64(pFriendPlayer->get_user_gold());
			if ( pFriendPlayer->get_table_id() > 0 )
			{
				stream_out.add_byte(2);
			}
			else
			{
				stream_out.add_byte(1);
			}
			stream_out.add_int(pFriendPlayer->get_table_id());
		}
		else {
			stream_out.add_int64(pUserInfo->user_score.gold);
			stream_out.add_byte(0);
			stream_out.add_int(0);
		}
	}
	else {
		stream_out.add_int64(pUserInfo->user_score.gold);
		stream_out.add_byte(0);
		stream_out.add_int(0);
	}

	tmplen = strlen(pUserInfo->char_sign);
	stream_out.add_word(tmplen);
	stream_out.appand(pUserInfo->char_sign, tmplen);
	FrontClientHandler * pHandle = get_handler();
	if (pHandle)
	{
		printf( "MSG_NOTIFY_SEND_ONE_FRIEND========= stream_out.get_buffer_len() = %d\r\n", stream_out.get_buffer_len() );
		pHandle->sendPacket( MSG_NOTIFY_SEND_ONE_FRIEND, tmpdata, stream_out.get_buffer_len() );
	}
}

//通知用户消息
void Player::NotifyStatusToFriend( BYTE status )
{
	vector<FriendInfoEx>::iterator iter;
	for ( iter = m_friend_list.begin(); iter != m_friend_list.end(); iter++ )
	{
		FrontClientHandler * pFriendHandle = ActiveConnsList::instance()->find( iter->userid );
		if ( pFriendHandle )
		{
			FriendStatus playerstatus = {0};
			playerstatus.tableid = (get_table_id() == INVALID_TABLE) ? 0 : get_table_id();
			playerstatus.status = status;
			playerstatus.userid = m_user_info.user_id;
			pFriendHandle->sendPacket( MSG_NOTIFY_FRIEND_STATUS, (u8_t*)&playerstatus, sizeof(playerstatus) );
			printf("Player::NotifyStatusToFriend  table_id:%d \n", playerstatus.tableid);
		}
	}


	std::list<FriendInfo*>::iterator em_iter;
	for ( em_iter = m_enemy_list.begin(); em_iter != m_enemy_list.end(); em_iter++ )
	{
		FrontClientHandler * pFriendHandle = ActiveConnsList::instance()->find( (*em_iter)->userid );
		if ( pFriendHandle )
		{
			FriendStatus playerstatus = {0};
			playerstatus.tableid = (get_table_id() == INVALID_TABLE) ? 0 : get_table_id();
			playerstatus.status = status;
			playerstatus.userid = m_user_info.user_id;
			pFriendHandle->sendPacket( MSG_NOTIFY_EM_STGATUS, (u8_t*)&playerstatus, sizeof(playerstatus) );

		}
	}

	
}

void Player::OnGameOver(Player * win_player, int lost_gold)
{
	FrontClientHandler * pHandle = get_handler();
	if (win_player == NULL || m_enemy_list.empty() || pHandle == NULL)
		return;

	bool is_new = false;
	FriendInfo * pEnemy = GetEnemyFromList(win_player->get_user_id());
	if (pEnemy == NULL) {
		pEnemy = m_enemy_list.front();
		m_enemy_list.pop_front();
		is_new = true;
	}

	if (pEnemy == NULL)
		return;

	UserInfo & refuserinfo = win_player->getUserInfo();

	if (pEnemy->userid > 0)
		pHandle->DeleteEnemy(pEnemy->userid);

	pEnemy->userid = win_player->get_user_id();
	pEnemy->add_time = time(NULL);
	pEnemy->sex = refuserinfo.user_sex;
	pEnemy->golds = lost_gold;
	memcpy(pEnemy->nick_name, refuserinfo.nick_name, sizeof(refuserinfo.nick_name));
	memcpy(pEnemy->head_picture, refuserinfo.head_picture, sizeof(refuserinfo.head_picture));
	memcpy(pEnemy->char_sign, refuserinfo.char_sign, sizeof(refuserinfo.char_sign));

	if (!is_new)
		pHandle->UpdateOrAddEnemy(1, pEnemy);
	else
		pHandle->UpdateOrAddEnemy(0, pEnemy);

	m_enemy_list.push_back(pEnemy);
}

FriendInfo * Player::GetEnemyFromList(DWORD userid)
{
	FriendInfo * pEnemy = NULL;
	std::list<FriendInfo *>::iterator it = m_enemy_list.begin();
	for (; it != m_enemy_list.end(); ++it) {
		pEnemy = *it;
		if (pEnemy == NULL)
			continue;

		if (pEnemy->userid != userid)
			continue;

		m_enemy_list.erase(it);
		return pEnemy;
	}

	return NULL;
}

void Player::SendEnemyToClient()
{
	u8_t  tmpdata [ 8192 ]	;
	StreamWriter stream_out(tmpdata, sizeof(tmpdata));

	WORD add_num = 0;

	for (int i = 0; i < MAX_ENEMY_COUNT; ++i) {
		if (m_enemy_info[i].userid <= 0)
			continue;

		add_num++;
	}

	stream_out.add_word(add_num);
	
	FrontClientHandler * pFriendHandle = NULL;
	Player * pFriendPlayer = NULL;
	WORD tmplen = 0;
	for (int i = 0; i < MAX_ENEMY_COUNT; ++i) {
		if (m_enemy_info[i].userid <= 0)
			continue;
		pFriendHandle = ActiveConnsList::instance()->find(m_enemy_info[i].userid);	
		//printf( "zq enemy handle = %x\r\n", pFriendHandle );
		if ( (pFriendHandle) && ( pFriendHandle->getPlayer()) )
		{
			memcpy( m_enemy_info[i].nick_name, pFriendHandle->getPlayer()->get_nick_name(), sizeof(m_enemy_info[i].nick_name) );
			memcpy( m_enemy_info[i].char_sign, pFriendHandle->getPlayer()->get_char_sign(), sizeof(m_enemy_info[i].char_sign) );
			m_enemy_info[i].sex = pFriendHandle->getPlayer()->get_sex();
		}

		stream_out.add_uint( m_enemy_info[i].userid ) ;
		stream_out.add_int64( m_enemy_info[i].add_time ) ;

		tmplen = strlen(m_enemy_info[i].nick_name);
		stream_out.add_word(tmplen);
		stream_out.appand(m_enemy_info[i].nick_name, tmplen);

		tmplen = strlen(m_enemy_info[i].head_picture);
		stream_out.add_word(tmplen);
		stream_out.appand(m_enemy_info[i].head_picture, tmplen);

		stream_out.add_byte(m_enemy_info[i].sex);

		
		if (pFriendHandle != NULL) {
			pFriendPlayer = pFriendHandle->getPlayer();
			if (pFriendPlayer) {
				stream_out.add_int64(m_enemy_info[i].golds);
				stream_out.add_byte(1);
				stream_out.add_int(pFriendPlayer->get_table_id());
				stream_out.add_int(pFriendPlayer->GetCurRoomID());
			}
			else {
				stream_out.add_int64(m_enemy_info[i].golds);
				stream_out.add_byte(0);
				stream_out.add_int(0);
				stream_out.add_int(0);
			}
		}
		else {
			stream_out.add_int64(m_enemy_info[i].golds);
			stream_out.add_byte(0);
			stream_out.add_int(0);
			stream_out.add_int(0);
		}
		//printf( "zq enemy gold %lld====\r\n",  m_enemy_info[i].golds );
		tmplen = strlen(m_enemy_info[i].char_sign);
		stream_out.add_word(tmplen);
		stream_out.appand(m_enemy_info[i].char_sign, tmplen);
		//printf( "zq req enemy %d\r\n",m_enemy_info[i].userid );
	}

	printf( "zq rsp enemy stream_out.get_buffer_len() %d\r\n", stream_out.get_buffer_len() );
	FrontClientHandler * pHandle = get_handler();
	if (pHandle)
		pHandle->sendPacket( MSG_RESP_ENEMY_LIST_RESULT, tmpdata, stream_out.get_buffer_len() );
}

void Player::InitEnemyFromDB(char * pData, int data_len)
{
	memset(m_enemy_info, 0, sizeof(m_enemy_info));

	if (m_enemy_list.empty())
		return;

	if (pData == NULL || data_len < 2) 
		return;

	StreamReader reader( pData );
	WORD enemy_count = reader.get_short();
	if (enemy_count == 0)
		return;

	FriendInfo * pFriend = NULL;
	WORD tmplen;
	for (WORD i = 0; i < enemy_count; ++i) {

		pFriend = m_enemy_list.front();
		m_enemy_list.pop_front();
		if (pFriend == NULL)
			continue;

		pFriend->userid = reader.get_uint();
		pFriend->add_time = reader.get_int64();

		tmplen = reader.get_ushort();
		if (tmplen >= NAME_LENGTH)
			tmplen = NAME_LENGTH - 1;

		memcpy(pFriend->nick_name, reader.get_current_ptr(), tmplen);

		reader.skip(tmplen);

		tmplen = reader.get_ushort();
		if (tmplen >= PICTURE_HEAD_LENGTH)
			tmplen = PICTURE_HEAD_LENGTH - 1;

		memcpy(pFriend->head_picture, reader.get_current_ptr(), tmplen);
		reader.skip(tmplen);

		pFriend->golds = reader.get_uint();
		pFriend->sex = reader.get_byte();

		tmplen = reader.get_ushort();
		if (tmplen >= SIGN_MAX_LEN)
			tmplen = SIGN_MAX_LEN - 1;

		memcpy(pFriend->char_sign, reader.get_current_ptr(), tmplen);
		reader.skip(tmplen);
		//printf( "zq enemy %d \r\n", pFriend->userid );
		m_enemy_list.push_back(pFriend);
	}
}



int	Player::GetGiftCount(int kindtype)
{
	switch (kindtype)
	{
	case 1:
		return m_user_info.flower;
	case 2:
		return m_user_info.aggs;
	case 3:
		return m_user_info.car;
	case 4:
		return m_user_info.villa;
	case 5:
		return m_user_info.yacht;
	default:
		break;
	}
	return 0;
}

void Player::ChangeGiftCount(int kindtype, int num, bool is_add)
{
	DWORD * pDesNum = NULL;
	switch (kindtype)
	{
	case 1:
		pDesNum = &m_user_info.flower;
		break;
	case 2:
		pDesNum = &m_user_info.aggs;
		break;
	case 3:
		pDesNum = &m_user_info.car;
		break;
	case 4:
		pDesNum = &m_user_info.villa;	
		break;
	case 5:
		pDesNum = &m_user_info.yacht;
		break;
	default:
		break;
	}
	
	if (pDesNum == NULL)
		return;

	int curnum = (int)(*pDesNum);

	if (is_add) 
		curnum += num;
	else
		curnum -= num;

	if (curnum <= 0)
		curnum = 0;

	*pDesNum = (DWORD)curnum;
}

void Player::SetGiftCount(int giftID,int giftNum)
{
	switch (giftID)
	{
	case 1:
		 m_user_info.flower = giftNum;
		 break;
	case 2:
		 m_user_info.aggs = giftNum;
		 break;
	case 3:
		 m_user_info.car = giftNum;
		 break;
	case 4:
		m_user_info.villa = giftNum;
		 break;
	case 5:
		  m_user_info.yacht = giftNum;
		 break;
	default:
		break;
	}
	return;
}

int Player::OnLotteryGameEnd(BYTE & card_type)
{
	if (!m_lottery_info.is_start)
		return 0;

	int need_gold = m_lottery_info.GetNeedGold();
	int reward_gold = GetLotteryRewardGold(card_type);

	int change_gold = reward_gold - need_gold;
	long long cur_gold = get_user_gold();
	cur_gold += change_gold;
	if (cur_gold <= 0) 
		cur_gold = 0;

	set_user_gold(cur_gold);
	m_user_info.tiger_raward_pool += need_gold;
	m_user_info.tiger_raward_pool -= reward_gold;

	return reward_gold;
}

int	Player::GetLotteryRewardGold(BYTE & card_type)
{
	GoodCardManager cgoodcard;
	BYTE tmpCard[MAX_COUNT] = {0};
	memcpy( tmpCard, m_lottery_info.lottery_card, 3 );
	
	card_type = cgoodcard.GetCardType( tmpCard );
	int reward_gold = 0;
	switch (card_type)
	{
	case CT_DOUBLE:
		reward_gold = m_lottery_info.buygold*2;
		break;
	case CT_SHUN_ZI:
		reward_gold = m_lottery_info.buygold*5;
		break;
	case CT_JIN_HUA:
		reward_gold = m_lottery_info.buygold*3;
		break;
	case CT_SHUN_JIN:
		reward_gold = m_lottery_info.buygold*16;
		break;
	case CT_BAO_ZI: 
		{
			if ( cgoodcard.IsWangPai( tmpCard ) )
			{
					reward_gold = m_lottery_info.buygold*25;
					card_type = CT_WANG_PAI;
			}
			else
			{
				reward_gold = m_lottery_info.buygold*16;
			}
		}
		break;
	default: 
		{
			card_type = CT_SINGLE;
			//特殊235
			{
				if ( cgoodcard.IsDiLong( tmpCard ) )
				{
					reward_gold = m_lottery_info.buygold*12;
					card_type = CT_DI_LONG;
					break;
				}

				int lucky_num = 0;
				for ( int i = 0; i < 3; i++ )
				{
					int huase = ((tmpCard[i] & 0xf0) >> 4);
					if ( huase == m_lottery_info.lucky_color )
					{
						lucky_num++;
					}
				}
				if (lucky_num == 1)
				{
					reward_gold = m_lottery_info.buygold*0.2;
					card_type = CT_YI_HUA;
				}
				else if (lucky_num == 2)
				{
					reward_gold = m_lottery_info.buygold*0.5;
					card_type = CT_ER_HUA;
				}
				else
				{
					reward_gold = m_lottery_info.buygold*0;
					card_type = CT_NO_HUA;
				}
			}
		}
		break;
	}

	return reward_gold;
}


















